package com.zhaopin.theswordreferstooffer;

/**
 * @author yin.peide
 * @date 2021-06-21 17:50
 * 面试题32： 从1到n整数中1出现的次数
 */
public class Offer32 {
    public static void main(String[] args) {

        int p = 21345;
        System.out.println(getInt1(p));
    }


    /**
     * 此种方法为不考虑时间效率的解法
     */
    public static int getInt1(int p) {
        int number = 0;
        for (int i = 1; i <= p; i++) {
            number = number + traverse1(i);
        }

        return number;
    }


    public static int traverse1(int p) {
        int number = 0;
        while (p > 0) {
            if (p % 10 == 1) {
                number++;
            }
            p = p / 10;
        }
        return number;
    }

}
